Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Python match case shows error with list element as case. Expected ":"

Hi I was using the newly added match case function but I encountered this problem.

Here's my code:

from typing import List

class Wee():
    def __init__(self) -> None:
        self.lololol: List[str] = ["car", "goes", "brrrr"]

    def func1(self):
        self.weee = input()
        try:
            match self.weee:
                case self.lololol[0]:
                    print(self.lololol[0])
                case self.lololol[1]:
                    print(self.lololol[1])
                case _:
                    print(self.lololol[2])
        except SyntaxError as e:
            print(e)

waa = Wee()
waa.func1()

At line 11 and 13, errors show up saying SyntaxError: expected ':'. However, when I change case self.lololol[0]: to case "car":, the errors disappear. What is happening?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can’t use arbitrary expressions as patterns (since that would sometimes be ambiguous), only a certain subset.

If you want to match against the elements of a list, you should probably, depending on the situation, either make them separate variables, or use list.index.

over 4 years ago · Santiago Trujillo Relatório

0

This is because the match case statement is intended for structural matching. Specifically PEP 635 says:

Although patterns might superficially look like expressions, it is important to keep in mind that there is a clear distinction. In fact, no pattern is or contains an expression. It is more productive to think of patterns as declarative elements similar to the formal parameters in a function definition.

Specifically, plain variables are used as capture patterns and not as match patterns - definitely not what a C/C++ programmer could expect... - and functions or subscripts or just not allowed.

On a practical point of view, only litteral or dotted expressions can be used as matching patterns.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda